import LXCOM from '../common/common.js'
import absgraph from '../graph/absgraph.js'
import { CoordTRFUtil } from '../common/tools/coordtool.js'
import Coord from '../common/struct/coord.js'
import ActPkg from '../graph/action/circleaction.js'
import {FillUtil} from '../graph/paintutil/fillutil.js'
import {LGXEnums} from '../common/sysenum.js'
import {Strokeutil} from '../graph/paintutil/strokeutil.js'
import fillinfo from "../common/struct/fillinfo";

class TerminalGraph extends absgraph.AbsGraph {
    constructor(gid) {
        super(gid);
        var p = new Coord.PointPos();
        this.setPos(p);

        var action = new ActPkg.CircleGraphAction(this);
        this.setActionSvc(action);
        this.radius = 1.5;
        this.m_graphType = LGXEnums.LGXGraphType.TerminalGraph_type;
        this._lineDef = new LXCOM.LGXLineDefInfo();
        this._fillDef = new fillinfo.LGXFillDefInfo();
    }

    display(map, mapInfo,forClip) {
        var scaling = mapInfo.getScaling();
        var disRadius = scaling * this.radius;
        var x = this._position.x;
        var y = this._position.y;
        var cntpt = { x, y };
        var p1 = CoordTRFUtil.world2Screen(cntpt, mapInfo);

        map.lineWidth = this._lineDef.lineWidth;
        map.beginPath();
        map.arc(p1.x, p1.y, disRadius, 0, 2 * Math.PI);
        map.closePath();
        Strokeutil.active(this._lineDef,this,map,mapInfo);
        if (!forClip)
        FillUtil.active(this._fillDef, map,this,mapInfo,false);

        super.displayGraphScope(map, mapInfo);
        this.displayFeaturePoint(map, mapInfo);
    }

    displayFeaturePoint(map, mapInfo) {
        if (!this._selected) {
            return;
        }
        var ptLst = this.getFeaturePtLst();
        for (var i = 0; i < ptLst.length; i++) {
            var point = ptLst[i];
            var p1 = CoordTRFUtil.world2Screen(point, mapInfo);
            var x = p1.x;
            var y = p1.y;
            super.drawFocusPoint(map, x, y, "#0000ff");
        }
    }

    disInSymbol(transformInfo,map, mapInfo,forClip) {
        var scaling = mapInfo.getScaling();
        var disRadius = scaling * this.radius;
        var disLongRadius = disRadius;
        var p1x = this._position.x * scaling;
        var p1y = -this._position.y * scaling;

        map.beginPath();
        map.lineWidth = this._lineDef.lineWidth*scaling;
        map.strokeStyle = "#ee00cc";
        map.arc(p1x, p1y, disLongRadius, 0, 2 * Math.PI);
        map.closePath();
        if (!forClip){
            Strokeutil.active(this._lineDef,this,map,mapInfo);
            FillUtil.active(this._fillDef, map,this,mapInfo,true);
        }
    }

    distance(tPoint, scaling, mapscale) {
        if (!super.preCheckInScope(tPoint, scaling)) {
            return -1;
        }
        else {
            return 0;
        }
    }

    calculateSelKind(tPoint, scaling, mapscale) {
        if (!super.preCheckInScope(tPoint, scaling)) {
            return 0;
        }
        else {
            return 1;
        }
    }

    calScope(mapscale) {
        var position = this.getPos();
        var nLineWid = this._lineDef.lineWidth;
        this._zone.left = (position.x - this.radius - nLineWid);
        this._zone.bottom = (position.y - this.radius - nLineWid);
        this._zone.right = (position.x + this.radius + nLineWid);
        this._zone.top = (position.y + this.radius + nLineWid);
    }

    getFeaturePtLst() {
        var ptlst = new Array;

        var p = new LXCOM.LGXPoint();
        p.x = this.pos.x;
        p.y = this.pos.y;
        ptlst.push(p);
        return ptlst;
    }

    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }
        this.pos.x += tOffset.x;
        this.pos.y += tOffset.y;
    }

    getMainFeaturePTLst(tFeaturePTLst) {
        var posList = this.getFeaturePtLst();
        for (var i = 0; i < posList.length; i++) {
            var pt = posList[i];
            tFeaturePTLst.push({pt,kind:LGXEnums.AuxPointKind.center});
        }
    }

    assignInfo(obj) {
        this.m_gid = obj.gid;
        this.type = obj.type;
        this._position.clone(obj.pos);
        this._lineDef.color.setColorByValue(obj.color);
    }

    modifyAttribute(obj, mapscale) {
        super.modifyCommonAttribute(obj);
        this.calScope(mapscale);
    }
}

export default { TerminalGraph }
